package business.entity;

public class VerifyCard implements IVerifyCard {
	private Card card = null;

	@Override
	public Card getCard() {
		return card;
	}

	@Override
	public String verifyCardQQ() {
		// QQ规则
		if(card == null) return null;
		String qq = card.getQq();
		String rule = "\\d{0,20}";
		if (qq == null || "".equals(qq)) {
			return null;
		} else if (!qq.matches(rule)) {
			if(qq.length() > 20){
				return "QQ太长了";
			}else{
				return "QQ号码只能是数字，不能是其它字符";
			}
		}
		return null;
	}

	@Override
	public String verifyCardName() {
		// 名字不能为空
		if(card == null) return null;
		String name = card.getName();
		if (name == null || "".equals(name)) {
			return "名字不能为空";
		} else if (name.length() > MAX_NAME_LEN) {
			return "名字长度不能大于" + MAX_NAME_LEN;
		}
		return null;
	}

	@Override
	public String verifyCardWeibo() {
		// TODO
		if(card == null) return null;
		String weibo = card.getWeibo();
		if (weibo == null || "".equals(weibo)) {
			return null;
		}
		if (weibo.length() < 4 || weibo.length() > MAX_WEIBO_LEN) {
			return "微博的昵称的长度要在" + 4 + "和" + MAX_WEIBO_LEN + "之间";
		}
		return null;
	}

	@Override
	public String verifyCardEmail() {
		if(card == null) return null;
		String email = card.getEmail();
		String rulePattern = "^([a-zA-Z0-9_\\-\\.]+)@"
				+ "((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|("
				+ "([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		if (email == null || "".equals(email)) {
			return null;
		} else if (email.length() > MAX_EMAIL_LEN) {
			return "邮箱的名称太长";
		} else if (!email.matches(rulePattern)) {
			return "输入不符合邮箱的格式";
		}
		return null;
	}

	@Override
	public void setCard(Card card) {
		this.card = card;
	}

	@Override
	public String verifyCardTelephone() {
		// 电话的规则
		if(card == null) return null;
		String telephone = card.getTelephone();
		String rule = "[\\d | -]{0,20}";
		if (telephone == null || "".equals(telephone)) {
			return null;
		} else if (!telephone.matches(rule)){
			if (telephone.length() > 20){
				return "号码过长";
			}else{
				return "号码只能用数字和-";
			}
		}
		return null;
	}
}
